//package com.zmn.plat.task.job.channel;
//
//import com.alibaba.fastjson.JSON;
//import com.github.ltsopensource.core.domain.Action;
//import com.github.ltsopensource.core.domain.Job;
//import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
//import com.github.ltsopensource.spring.tasktracker.LTS;
//import com.github.ltsopensource.tasktracker.Result;
//import com.google.common.base.Joiner;
//import com.zmn.common.constant.StatusConsts;
//import com.zmn.common.dto2.ResponseDTO;
//import com.zmn.common.utils.date.DateUtil;
//import com.zmn.manager.lts.interfaces.task.TaskManager;
//import com.zmn.mcc.dubbo.dto.DubboConsts;
//import com.zmn.oms.common.dio.normal.query.OrderWorkQueryDIO;
//import com.zmn.oms.dubbo.interfaces.normal.order.NormalOrderWorkListRemoteService;
//import com.zmn.plat.business.interfaces.channel.ChannelBService;
//import com.zmn.plat.manager.dictionary.ChannelDict;
//import com.zmn.plat.manager.utils.TaskConsts;
//import com.zmn.plat.model.entity.channel.Channel;
//import com.zmn.plat.model.entity.channel.ChannelQuery;
//import com.zmn.plat.services.interfaces.channel.ChannelService;
//import org.apache.dubbo.config.annotation.Reference;
//import org.apache.poi.util.StringUtil;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.Resource;
//import java.util.*;
//
///**
// * 类描述：每月1号定时自动停用连续12个月无订单渠道
// *
// * @author yangshuangping
// * @since 2021/06/22 17:59
// */
//@LTS
//public class ChannelClosureJob {
//    private Logger log = LoggerFactory.getLogger(ChannelClosureJob.class);
//
//    @Resource
//    TaskManager taskManager;
//
//    @Autowired
//    private ChannelService channelService;
//
//    @Autowired
//    protected ChannelBService channelBService;
//
//    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
//    NormalOrderWorkListRemoteService normalOrderWorkListRemoteService;
//
//    /**
//     * 描述：每月1号定时自动停用连续12个月无订单渠道  0 0 0 1 * ? 每月的凌晨执行
//     *
//     * @author yangshuangping
//     * @since 2021/06/22 17:59
//     */
//    @PostConstruct
//    void init() {
//        Job job = new Job();
//        job.setTaskId(TaskConsts.TASK_ID_SHUTDOWN_WITHOUT_A_SINGLE_CHANNEL);
//        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_SHUTDOWN_WITHOUT_A_SINGLE_CHANNEL);
//        job.setCronExpression("0 0 0 1 * ?");
//        job.setReplaceOnExist(true);
//        ResponseDTO<String> result = taskManager.submitTask2(job);
//        if (!result.isSuccess() || !Objects.equals(result.getStatus(), StatusConsts.STATUS_SUCCESS)) {
//            log.info("#ShutDownWithoutASingleChannel# job submit fail...");
//        }
//    }
//
//    /**
//     * 执行job
//     *
//     * @param job the job
//     * @return result
//     */
//    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_SHUTDOWN_WITHOUT_A_SINGLE_CHANNEL)
//    public Result runShutDownWithoutASingleChannelJob(Job job) {
//        log.info("#job#ShutDownWithoutASingleChannel# start running... job:[{}]", job.getTaskId());
//        List<Integer> closeIdsList = new ArrayList<>();
//        // 获取一年前的时间
//        String now = DateUtil.dateFormatToString(new Date(), DateUtil.FORMAT_DEFAULT);
//        Date startDate = DateUtil.getBaseDefer(now, DateUtil.FORMAT_DEFAULT, -1, 0, 0, 0, 0, 0);
//        ChannelQuery query = new ChannelQuery();
//        query.setRank(ChannelDict.RANK_TWO);
//        query.setStatus(ChannelDict.STATUS_NORMAL);
//        query.setCreatTime(DateUtil.dateFormatToString(startDate, DateUtil.FORMAT_DEFAULT));
//        log.error("#job#ShutDownWithoutASingleChannel#requiredDubboParameter[{}]", JSON.toJSONString(query));
//        // 获取所有有效的二级渠道
//        List<Channel> list = Optional.ofNullable(channelService.listByQuery(query)).orElse(new ArrayList<>());
//        log.info("#job#ShutDownWithoutASingleChannel# EFFECTIVE-CHANNEL :{} 条 ChannelID【{}】", list.size());
//        OrderWorkQueryDIO queryDIO = new OrderWorkQueryDIO();
//        queryDIO.setReceiveTime(DateUtil.dateFormatToString(startDate, DateUtil.FORMAT_DEFAULT));
//        for (Channel channel : list) {
//            // 根据渠道ID查询是否连续12个月未出单
//            queryDIO.setChannelIdList(Collections.singletonList(channel.getChannelId()));
//            int count = normalOrderWorkListRemoteService.countOrderWorkByQuery(queryDIO).getData();
//            if (count == 0) {
//                closeIdsList.addAll(closeChannel(channel));
//            }
//        }
//        log.info("#job#ShutDownWithoutASingleChannel#end running... closeChannel count :{} 条 channelIDs【{}】"
//                , closeIdsList.size(), Joiner.on(",").join(closeIdsList));
//        return new Result(Action.EXECUTE_SUCCESS, "自动停用连续12个月无订单渠道,共[" + closeIdsList.size() + "]条");
//    }
//
//    /**
//     * 关闭二级渠道时判断是否需要关闭一级渠道
//     *
//     * @param channel 渠道信息
//     * @return
//     */
//    private Collection<? extends Integer> closeChannel(Channel channel) {
//
//        List<Integer> list = new ArrayList<>();
//        list.add(channel.getChannelId());
//        // 判断是否需要关闭对应的一级渠道，需要过滤掉当前二级渠道
//        Long count = channelService.listByParentId(channel.getParentId()).stream()
//                // 过滤状态为开启中
//                .filter(channel1 -> Objects.equals(channel1.getStatus(), ChannelDict.STATUS_NORMAL))
//                // 排除当前要关闭的渠道
//                .filter(channel1 -> !channel1.getChannelId().equals(channel.getChannelId())).count();
//        if (count == 0) {
//            list.add(channel.getParentId());
//        }
//        // 关闭渠道
//        channelBService.modifyStatusByIds(list, ChannelDict.STATUS_HIDDEN, "system");
//        log.info("#job#ShutDownWithoutASingleChannel# executing... closeChannel channelIds :[{}]", StringUtil.join(",", list));
//        return list;
//    }
//}
